#include "Particle.h"
#include <GL/glut.h>
#include "BMPLoader.h"
#include <iostream>
#include <vector>
#include "Collidable.h"

using namespace std;

const int Particle::MAX_COUNT = 100;
const int Particle::MIN_COUNT = 20;
const int Particle::LIFETIME = 100;
const int Particle::SPEED = 15;

GLuint Particle::glowTexture = -1;

Particle::Particle() : dampening(0.5), age(0), alpha(1), dead(false)
{
	if (glowTexture == -1){
	BMPClass bmp;
	BMPLoad("glow.bmp",bmp);
	
	glGenTextures(1, &glowTexture);
	glBindTexture(GL_TEXTURE_2D, glowTexture);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
	
	glTexImage2D(GL_TEXTURE_2D,0,3,bmp.width,bmp.height,0,GL_RGB,GL_UNSIGNED_BYTE,bmp.bytes);
	}
}

void Particle::move(const std::vector<Collidable*>& plats)
{

	if (age > LIFETIME)
	{
		alpha -= 0.01;
		if ( alpha <= 0 )
			dead = true;
	}else{
		age++;
	}
	position.x += xVel;
	Collidable* collision = this->checkCollision(plats);
	if (collision){
		position.x -= xVel;
		xVel = -(xVel * dampening);
		// move in opposite direciton now?
	}

	yVel -= 0.6;		//gravity
	position.y += yVel;
	collision = this->checkCollision(plats);
	if (collision){
		position.y -= yVel;
		yVel = -(yVel * dampening);
		xVel *= dampening;
	}
}

void Particle::draw()
{
	// DRAW Particle GLOW
	
	glEnable(GL_TEXTURE_2D);
	glDisable(GL_LIGHTING);
	glColor4f(0.8,0.8,1,alpha);
	//glColor4f(1.0,.0f,.0f,alpha);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	glTexEnvi(GL_TEXTURE_ENV, 
			GL_TEXTURE_ENV_MODE, GL_MODULATE); 
	
	glBindTexture(GL_TEXTURE_2D, glowTexture);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glTranslatef(position.x, position.y, 0);
	glScalef(sx,sy,sz);
	
	glBegin(GL_QUADS);
		glTexCoord2f(1.0,1.0);
		//glNormal3f(0.0f, 0.0f, 1.0f);
		glNormal3f(0.0f, 0.0f, 0.0f);
		glVertex3f( cw, ch, 0);
		glTexCoord2f(0.0,1.0);
		glVertex3f(-cw, ch, 0);
		glTexCoord2f(0.0,0.0);
		glVertex3f(-cw, -ch, 0);
		glTexCoord2f(1.0,0.0);
		glVertex3f( cw, -ch, 0);
	glEnd();
	glPopMatrix();
	glDisable(GL_BLEND);
	glEnable(GL_LIGHTING);
	glDisable(GL_TEXTURE_2D);
}

Collidable* Particle::checkCollision(const std::vector<Collidable*>& plats)
{
	for (std::vector<Collidable*>::const_iterator it = plats.begin(); it != plats.end(); it++)
	{
		if ( (*it)->type == ENEMY )
		{
			continue;
		}

		float sxB = (*it)->sx;

		float leftB = (*it)->position.x - ( ((*it)->cw) * sxB );
		if (position.x <= leftB)
			continue;
		float rightB = (*it)->position.x + ( ((*it)->cw) * sxB );
		if (position.x >= rightB)
			continue;

		float syB = (*it)->sy;

		float topB = (*it)->position.y + ( ((*it)->ch) * syB );
		if (position.y >= topB)
			continue;
		float bottomB = (*it)->position.y - ( ((*it)->ch) * syB );
		if (position.y <= bottomB)
			continue;


		return (*it);


		/*
		float sxB = (*it)->sx;
		float syB = (*it)->sy;
		float leftB = (*it)->position.x - ( ((*it)->cw) * sxB );
		float topB = (*it)->position.y + ( ((*it)->ch) * syB );
		float rightB = (*it)->position.x + ( ((*it)->cw) * sxB );
		float bottomB = (*it)->position.y - ( ((*it)->ch) * syB );
	
		if ( (position.x < rightB) && (position.x > leftB) && (position.y > bottomB) && (position.y < topB) )
		{
			return (*it);
		}
		*/
	}
	return NULL;
	//return false;
}